var express = require('express');
var router = express.Router();

// const {getGoodsSell,getGoodsId,deleteGoodsSell,updateGoodsSell} = require("../service/goodsService")
const { getGoodsecharts, getGoods,addGoods ,
  deleteGoods,updateGoods,getGoodsSell,getGoodsId,deleteGoodsSell,updateGoodsSell,puton} = require("../service/goodsService")

// 设置前后端接口的二级路径

/**
 * ++++++++++++++++++++++++++++++++++刘磊+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 */

//商品图片的上传
const { uploadFiles, moveFiles, removeFiles } = require("../utils/uploadFiles")
const images = uploadFiles({
  path: "./public/temp",  //设置上传成功后的图片存储路径
  key: "file",  //key的值与前端   formData.append("file",files[0]);中的file保持一致
  filesSize: 100
})

router.post("/uploadImages", function (req, res, next) {
  // console.info("111")
  // console.log("图片信息", req.body)

  images(req, res, function (err) {
    if (err) {
      // console.log(err.code)
      if (err.code == 'LIMIT_FILE_SIZE') {
        res.send({ success: false, msg: "文件过大！" })
      }
    } else {
      // console.info("111")
      // console.info(req.files)
      res.send({ success: true, filename: req.files.map(item => item.filename) })

    }
  })

})



// // 获取商品getGoods
// router.get('/getGoods', async function (req, res, next) {
//   // 接收前端发送过来的数据

//   const data = await getGoods(req.query)
//   res.send(data)
// });

/***
 * ++++++++++++++++++++++++++++++++++李兴乾+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 * 
 */
/** ==========================商品操作====================== */
//获取商品（state为sell的获取） lxq
router.get('/getGoodsSell', async function (req, res, next) {
  // 接收前端发送过来的数据
  console.log(1111111,req.query)
  const data = await getGoodsSell(req.query)
  res.send(data)
});
//下架商品
router.get('/deleteGoodsSell', async function (req, res, next) {
  res.send(await deleteGoodsSell(req.query));
});
//编辑时通过—id查找到商品
router.get('/getGoodsId', async function (req, res, next) {
  res.send(await getGoodsId(req.query));
});
//修改上架商品信息 updateGoodsSell
router.post('/updateGoodsSell', async function (req, res, next) {
  const { imagePath } = req.body;
  if (imagePath.indexOf('temp') > 0) {
    const filename = imagePath.split('/').pop();
    // 将 temp 中临时文件移动到 images 中做永久保存
    moveFiles({
      fromPath: './public/temp',
      toPath: './public/images',
      filename
    });
    const data = await updateGoodsSell({ ...req.body, imagePath: `./images/${filename}` });
    if (data) {
      removeFiles("./public/temp");
    }
    res.send(data);

  } else {
    const data = await updateGoodsSell(req.body);
    res.send(data);
  }

  // res.send(await updateGoodsSell({ ...req.body, imagePath: `./images/${filename}` }));
});

/** ==========================管理员操作====================== */
/***
 * 
 * 
 * ++++++++++++++++++++++++++++++++++刘磊+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 * 
 */

 //删除库存商品
 router.post('/deleteGoods',async function(req,res,next){
  const data = await deleteGoods(req.body)
  // console.log(111111111,data)
  res.send(data)
 })

// router.get('/getgoodsECharts', async function (req, res, next) {
//   // 接收前端发送过来的数据
//   const data = await getGoods(req.query)
//   res.send(data)
// });
router.get('/getgoodsECharts', async function (req, res, next) {
  // 接收前端发送过来的数据
  const data = await getGoodsecharts(req.query)
  res.send(data)
});





// 新增商品
router.post("/addGoods", async function (req, res, next) {
  const { imagePath } = req.body;
  if (imagePath) {
    const filename = imagePath.split('/').pop();
    // 将 temp 中临时文件移动到 images 中做永久保存
    moveFiles({
      fromPath: './public/temp',
      toPath: './public/images',
      filename
    });
    const data = await addGoods({ ...req.body, imagePath: `./images/${filename}` });
    if (data) {
      removeFiles("./public/temp");
    }
    res.send(data);

  } else {
    const data = await addGoods(req.body);
    res.send(data);
  }
})



// // 删除学生
// router.delete("/", async function (req, res, next) {
//   res.send(await deleteGoods(req.body))

// })



// // 修改学生
// router.post("/updateGoods", async function (req, res, next) {
//   console.log("修改学生" + req.body)
//   res.send(await updateGoods(req.body))

// })


//上架
router.post("/puton", async function (req, res, next) {
  // console.log("学生" + req.body)
  console.log(req.body)
  res.send(await puton(req.body))
})


//===============================徐芊===========================================================
//获取全部商品
router.get('/getGoods',async function (req,res,next) {
  console.log(req.query)
  res.send(await getGoods(req.query))
})

//===============================徐芊===========================================================

module.exports = router;
